<script setup>
import { ref ,onMounted} from 'vue';
import { useRouter } from 'vue-router';
import request from '@/utils/request.js'
import { useUserStore } from '@/stores/index.js'

const router = useRouter();
const userStore = useUserStore();
// 邀请消息数据
const invitations = ref([]);


// 匹配伙伴按钮事件
const matchPartner = () => {
  router.push('/partner/FindPartnerContent');
};
const getInvitations =async () => {
  const res = await request.get('/getInviteInfo',{
    params:{
      userId:userStore.userId
    }
  });
    invitations.value = res.data.inviteList;
    console.log(1);
    console.log(invitations.value);
}
const handleInvite = async (inviteId,action) => {
  const res = await request.post('/handleInvite',{
    inviteId:inviteId,
    action:action,
    userId:userStore.userId
  });
  if(res.status === 200){
    getInvitations();
  }
}
onMounted(() => {
  getInvitations();
})

</script>

<template>
  <div class="container">
    <!-- 匹配伙伴卡片 -->
    <el-card class="match-card">
      <div class="match-card-content">
        <div class="match-text">
          <h2>去匹配伙伴，大家一起进步吧</h2>
          <p>点击立即找到同课同学</p>
          <el-button type="success" @click="matchPartner" class="match-text-btn">立即匹配</el-button>
        </div>
        <div class="match-img">
          <img src="../../assets/images/head2.png" alt="匹配伙伴" />
        </div>
      </div>
    </el-card>

    <!-- 邀请消息区域 -->
    <el-card class="invitation-card">
      <template #header>
        <div class="invitation-header">
          <span>最新邀请</span>
        </div>
      </template>

      <div v-if="invitations.length === 0" class="empty-tip">
        <el-empty description="暂无邀请消息" />
      </div>

      <div v-else class="invitation-list">
        <div
          v-for="invite in invitations"
          :key="invite.id"
          class="invitation-item"
          :class="{ 'unread': invite.status === 'unread' }"
        >
          <div class="invite-info">
            <span class="sender"><strong>系统通知</strong></span>
            <span class="course">{{ invite.inviteContent }}</span>
            <span class="time">{{ invite.sendTime }}</span>
          </div>
          <div class="invite-actions">
            <el-button type="primary" size="small" @click="handleInvite(invite.inviteId,1)">接受</el-button>
            <el-button type="info" size="small"  @click="handleInvite(invite.inviteId,0)">拒绝</el-button>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 60%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.match-card {
  margin-bottom: 30px;
  padding: 25px;
  width: 100%;
}

.match-card-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 25px;
}


.match-text h2 {
  font-size: 50px;
  color: #1e40af;
  line-height: 1.5;
  font-weight: bold;
  font-family: "Ma Shan Zheng", "KaiTi", serif;
  letter-spacing: 2px;
  text-shadow: 1px 1px 2px rgba(44, 94, 46, 0.2);
  animation: floatMove 2s ease-in-out infinite alternate;
  margin: 0; /* 移除默认margin */
}

@keyframes floatMove {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}

.match-text p {
  font-size: 20px;
  color: #555;
  margin: 5px 0; /* 调整margin */
  font-family: "Microsoft YaHei", sans-serif;
  letter-spacing: 1px;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
  animation: floatMove 2s ease-in-out infinite alternate;
}

.match-img img {
  border-radius: 8px;
  width: 380px;
  height: auto;
  margin-right: 30px;
}

.match-text-btn {
  border: 1px solid #27ae60;
  border-radius: 20px;
  width: 100px;
  height: 40px;
  font-size: 16px;
  margin-top: 10px; /* 添加上边距 */
}
.invitation-card {
  width: 110%;
  margin-top: 20px;
}

.invitation-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.invitation-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.invitation-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-radius: 8px;
  transition: all 0.3s;
}

.invitation-item:hover {
  background-color: #f5f7fa;
}

.invitation-item.unread {
  background-color: #ecf5ff;
  border-left: 3px solid #409eff;
}

.invite-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.sender {
  font-weight: bold;
  color: #333;
}

.course {
  font-size: 14px;
  color: #666;
}

.time {
  font-size: 12px;
  color: #999;
}

.invite-actions {
  margin-left: 20px;
}

.empty-tip {
  padding: 40px 0;
  text-align: center;
  color: #999;
}
</style>
